#!/usr/bin/env python
import os,sys
import subprocess

Iso2Ids = {
    "U238":92238,
    "Th232":90232,
    "Ac227":89227,
    "Pb210":82210
}

if os.path.exists('inputs/alphaN/sources'):
    for root, dirs, files in os.walk('inputs/alphaN/sources'):
        if not len(root.split("/")) > 4:
            continue

        list_file_path = os.path.join(root[:root.rfind("/")], "valid_files.list")
        list_f = open(list_file_path, "r")
        valid_file_pool = [v.strip() for v in list_f.readlines()]
        list_f.close()

        for file in files:
            this_file = os.path.join(root[root.rfind("/")+1:], file)

            if not this_file in valid_file_pool:
                continue

            v_args = os.path.join(root.split("/")[-2], root.split("/")[-1])
            t_args = file.split("_")[0]
            i_args = Iso2Ids[file.split("_")[1][:-5]]
            d_args = file.split("_")[-1][:5]
            f_args = file.split("_")[2][5:]

            cmd = './u_generator -i %d -t %s -s %s -d %s -A -v %s -n 10' % (
                 i_args, t_args, f_args, d_args, v_args
            )
            print(cmd)
            process = subprocess.Popen(cmd, shell=True).wait()

